layui.define(['table', 'form','treeGrid'], function(exports){
        //var tableId='listTable';
        var editObj=null,ptable=null,treeGrid=null,tableId='listTable',layer=null;
        var $ = layui.$
        ,layer = layui.layer
        ,laytpl = layui.laytpl
        ,setter = layui.setter
        ,admin = layui.admin
        ,view = layui.view
        ,table = layui.table
        ,router = layui.router()
        ,form = layui.form
        ,treeGrid = layui.treeGrid;//很重要;
        ptable=treeGrid.render({
            id:tableId
            ,elem: '#'+tableId
            ,idField:'id'
            ,url:'/index/Jurisdiction/node_list'
            ,cellMinWidth: 100
            ,treeId:'id'//树形id字段名称
            ,treeUpId:'pid'//树形父id字段名称
            ,treeShowName:'title'//以树形式显示的字段
            ,heightRemove:[".dHead",10]//不计算的高度,表格设定的是固定高度，此项不生效
            ,height:'100%'
            ,isFilter:false
            ,iconOpen:false//是否显示图标【默认显示】
            ,isOpenDefault:false//节点默认是展开还是折叠【默认展开】
            ,cols: [[
                {type:'numbers'}
                ,{type:'radio'}
                ,{type:'checkbox'}
                ,{field:'title', title: '菜单名称'}
                ,{field:'name',title: '地址'}
                ,{field:'type', width:100, title: '类别'}
                ,{field:'condition', title: '规则表达式'}
                ,{field:'status', width:100,title: '状态', align:'center'/*toolbar: '#barDemo'*/
                    ,templet: function(d){
                        var html='';
                        // html='<input type="checkbox" name="status" value="'+d.status+'" lay-skin="switch" lay-text="打开|关闭" lay-filter="status" ';
                        // if (d.status== 1) {
                        //     html=html+' checked';
                        // }
                        // html=html+' >';
                        if (d.status== 1) {
                            html=html+' <button class="layui-btn layui-btn-xs">正常</button>';
                        }else{
                            html=html+' <button class="layui-btn layui-btn-primary layui-btn-xs">关闭</button>';
                        }
                        return html;
                    }
                }
                // ,{field:'id',width:100, title: 'id'}
                // ,{field:'pid', title: 'pid'}
                ,{width:150,title: '操作', align:'center'/*toolbar: '#barDemo'*/
                    ,templet: function(d){
                        var html='';
                        var addBtn='<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="add">添加</a>';
                        var editBtn='<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">修改</a>';
                        var delBtn='<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
                        return addBtn+editBtn+delBtn;
                    }
                }
            ]]
            ,page:false
            ,parseData:function (res) {//数据加载后回调
                return res;
            }
            ,onClickRow:function (index, o) {
                //console.log(index,o,"单击！");
            }
            ,onDblClickRow:function (index, o) {
                //console.log(index,o,"双击");
            }
        });

        treeGrid.on('tool('+tableId+')',function (obj) {
            if(obj.event === 'del'){//删除行
                del(obj);
            }else if(obj.event==="add"){//添加行
                add(obj);
            }else if(obj.event==="edit"){//添加行
                edit(obj);
            }

        });
        //监听展开操作
        form.on('switch(status)', function(obj){
            //修改状态
            layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
        });

    function del(obj) {
        layer.confirm("你确定删除数据吗？如果存在下级节点则一并删除，此操作不能撤销！", {icon: 3, title:'提示'},
            function(index){//确定回调 
                admin.req({
                    url: '/index/common/check_node?url=/Jurisdiction/del'
                    ,done: function(res){
                        if (res.code=='1002') {
                            layer.msg(res.msg, {icon: 2});
                        }else{
                            admin.req({
                                url: '/index/Jurisdiction/del'
                                ,type: 'post'
                                ,data: {id:obj.data.id}
                                ,done: function(res){
                                    console.log(res);
                                    obj.del();
                                    layer.close(index);
                                    
                                }
                            });
                        }
                    }
                });
            },function (index) {//取消回调
               layer.close(index);
            }
        );
    }


    // //一级以下添加
    function add(pObj) {
        admin.req({
            url: '/index/common/check_node?url=/Jurisdiction/add'
            ,done: function(res){
                if (res.code=='1002') {
                    layer.msg(res.msg, {icon: 2});
                }else{
                    admin.req({
                        url: '/index/Jurisdiction/add'
                        ,type: 'post'
                        ,data: {pid:pObj.data.id}
                        ,done: function(res){
                            //console.log(res);
                            //添加
                            var pdata=pObj?pObj.data:null;
                            var param={};
                            param.id=res.data.id;
                            param.title=res.data.title;
                            param.name=res.data.name;
                            param.type=res.data.type;
                            param.condition=res.data.condition;
                            param.status=res.data.status;
                            param.pid=pdata?pdata.id:null;
                            treeGrid.addRow(tableId,pdata?pdata[treeGrid.config.indexName]+1:0,param);
                        }
                    });
                }
            }
        });
    }
    function edit(pObj) {
        console.log(pObj);
        admin.req({
            url: '/index/common/check_node?url=/Jurisdiction/edit'
            ,done: function(res){
                if (res.code=='1002') {
                    layer.msg(res.msg, {icon: 2});
                }else{
                    admin.popup({
                        title: '编辑权限'
                        ,area: ['420px', '450px']
                        ,id: 'LAY-popup-user-add'
                        ,success: function(layero, index){
                            //console.log(data);数据信息
                            view(this.id).render('Jurisdiction/node/editform', pObj.data).done(function(){
                                form.render(null, 'layuiadmin-form-admin');
                                //监听提交
                                form.on('submit(LAY-user-back-submit)', function(data){
                                    var field = data.field; //获取提交的字段
                                    //console.log(field);
                                    //提交 Ajax 成功后，关闭当前弹层并重载表格
                                    admin.req({
                                        url: '/index/Jurisdiction/edit' //实际使用请改成服务端真实接口
                                        ,type: 'post'
                                        ,data: field
                                        ,done: function(res){
                                            if (res.code=='1002') {
                                                layer.msg(res.msg, {icon: 2});
                                            }else{
                                                treeGrid.reload(tableId);
                                            }

                                        }
                                    });
                                    layer.close(index); //执行关闭 
                                });
                            });
                        }
                    });

                }
            }
        });
    }

//     function print() {
//         console.log(treeGrid.cache[tableId]);
//         var loadIndex=layer.msg("对象已打印，按F12，在控制台查看！", {
//             time:3000
//             ,offset: 'auto'//顶部
//             ,shade: 0
//         });
//     }
    
//     function openorclose() {
//         var map=treeGrid.getDataMap(tableId);
//         var o= map['102'];
//         treeGrid.treeNodeOpen(tableId,o,!o[treeGrid.config.cols.isOpen]);
//     }


//     function openAll() {
//         var treedata=treeGrid.getDataTreeList(tableId);
//         treeGrid.treeOpenAll(tableId,!treedata[0][treeGrid.config.cols.isOpen]);
//     }

//     function getCheckData() {
//         var checkStatus = treeGrid.checkStatus(tableId)
//             ,data = checkStatus.data;
//         layer.alert(JSON.stringify(data));
//     }
//     function radioStatus() {
//         var data = treeGrid.radioStatus(tableId)
//         layer.alert(JSON.stringify(data));
//     }
//     function getCheckLength() {
//         var checkStatus = treeGrid.checkStatus(tableId)
//             ,data = checkStatus.data;
//         layer.msg('选中了：'+ data.length + ' 个');
//     }

//     function reload() {
//         treeGrid.reload(tableId,{
//             page:{
//                 curr:1
//             }
//         });
//     }
//     function query() {
//         treeGrid.query(tableId,{
//             where:{
//                 name:'sdfsdfsdf'
//             }
//         });
//     }
    
//     function test() {
// //        console.log(treeGrid.cache[tableId],treeGrid.getClass(tableId));
//         var map=treeGrid.getDataMap(tableId);
//         console.log(map);
//         // var o= map['102'];
//         // o.name="更新";
//         // treeGrid.updateRow(tableId,o);
//     }
    var active = {
        add: function(){
            admin.req({
                url: '/index/common/check_node?url=/Jurisdiction/add'
                ,done: function(res){
                    if (res.code=='1002') {
                        layer.msg(res.msg, {icon: 2});
                    }else{
                        admin.req({
                            url: '/index/Jurisdiction/add'
                            ,type: 'post'
                            ,data: {pid:0}
                            ,done: function(res){
                                //console.log(res);
                                //添加
                                var pObj=null;
                                var pdata=pObj?pObj.data:null;
                                var param={};
                                param.id=res.data.id;
                                param.title=res.data.title;
                                param.name=res.data.name;
                                param.type=res.data.type;
                                param.condition=res.data.condition;
                                param.status=res.data.status;
                                param.pid=0;
                                treeGrid.addRow(tableId,pdata?pdata[treeGrid.config.indexName]+1:0,param);
                            }
                        });
                    }
                }
            });
        }





    }

    $('.layui-btn.layuiadmin-btn-admin').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    //对外暴露的接口
    exports('jurisdiction_node', {});
});